package com.tn.openrpc.channel.secure;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.springframework.security.core.Authentication;

import com.tn.openrpc.channel.RemoteChannelRequest;

/**
 * <code>SecureRemoteChannelRequest</code> represents a secure request sent over a <code>RemoteChannel</code>.
 */
public class SecureRemoteChannelRequest extends RemoteChannelRequest
{
  private Authentication authentication;

  /**
   * Creates a new <code>RemoteChannelRequest</code>.
   *
   * @param authentication the authentication of the user making the request.
   * @param target         the target of the request.
   * @param methodName     the target method name.
   * @param args           the arguments.
   */
  public SecureRemoteChannelRequest(Authentication authentication, String target, String methodName, Object... args)
  {
    super(target, methodName, args);
    this.authentication = authentication;
  }

  /**
   * Returns the authentication.
   */
  public Authentication getAuthentication()
  {
    return authentication;
  }

  /**
   * Compares this object to another for equality.  For another object to be considered equal to a
   * <code>RemoteChannelRequest</code> the other object must be a <code>RemoteChannelRequest</code> and must have been
   * initialized with the same values.
   *
   * @param other the other object.
   * @return <code>true</code> if the objects are considered equal, otherwise <code>false</code>.
   */
  @Override
  public boolean equals(Object other)
  {
    return this == other || other != null && getClass() == other.getClass() &&
      new EqualsBuilder()
        .append(getAuthentication(), ((SecureRemoteChannelRequest)other).getAuthentication())
        .append(getTarget(), ((SecureRemoteChannelRequest)other).getTarget())
        .append(getMethodName(), ((SecureRemoteChannelRequest)other).getMethodName())
        .append(getArgs(), ((SecureRemoteChannelRequest)other).getArgs())
        .isEquals();
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public int hashCode()
  {
    return new HashCodeBuilder()
      .append(getAuthentication())
      .append(getTarget())
      .append(getMethodName())
      .append(getArgs())
      .toHashCode();
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public String toString()
  {
    return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
      .append("authentication", getAuthentication())
      .append("target", getTarget())
      .append("methodName", getMethodName())
      .append("args", getArgs())
      .toString();
  }
}
